package com.shchuangzhi.smartguard.core.command;

import com.google.gson.Gson;
import com.google.gson.annotations.SerializedName;
import com.shchuangzhi.smartguard.core.Consts;
import com.shchuangzhi.smartguard.core.InOutStats;
import com.shchuangzhi.smartguard.core.Machine;

import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Map;

/**
 * Created by j_hu on 5/7/17.
 */

public class SyncMetaCommand extends Command {

    private SyncSchoolHandler syncSchoolHandler;
    private SyncGradeHandler syncGradeHandler;
    private SyncClassHandler syncClassHandler;
    private SyncStudentHandler syncStudentHandler;
    private SyncTeacherHandler syncTeacherHandler;
    private SyncCardHandler syncCardHandler;
    private SyncStudentStateHandler syncStudentStateHandler;
    private SyncAutoModeSwitchTimeHandler syncAutoModeSwitchTimeHandler;
    private SyncInOutStatsHandler syncInOutStatsHandler;

    public SyncMetaCommand() {
        super(Consts.COMMAND_REQ_META_SYNC);
        syncSchoolHandler = new SyncSchoolHandler(this);
        syncGradeHandler = new SyncGradeHandler(this);
        syncClassHandler = new SyncClassHandler(this);
        syncStudentHandler = new SyncStudentHandler(this);
        syncTeacherHandler = new SyncTeacherHandler(this);
        syncCardHandler = new SyncCardHandler(this);
        syncStudentStateHandler = new SyncStudentStateHandler(this);
        syncAutoModeSwitchTimeHandler = new SyncAutoModeSwitchTimeHandler(this);
        syncInOutStatsHandler = new SyncInOutStatsHandler(this);
    }

    @Override
    protected OutputStream fill(OutputStream output) {
        DataOutputStream out = new DataOutputStream(output);
        try {
            CommandRQ<SyncMetaCommandRQ> rq = new CommandRQ<>();
            rq.setToken(getToken());
            rq.setData(new SyncMetaCommandRQ(getDbHelper().getSyncRecord().asMap()));
            out.write(new Gson().toJson(rq).getBytes());
        } catch(IOException e) {
            raiseError(e);
        }
        return out;
    }

    @Override
    protected String getType() {
        return "application/json";
    }

    @Override
    protected CommandHandler getHandler(String commandName) {
        if (commandName != null) {
            switch (commandName.toLowerCase()) {
                case Consts.COMMAND_RESP_SCHOOL: return syncSchoolHandler;
                case Consts.COMMAND_RESP_GRADE: return syncGradeHandler;
                case Consts.COMMAND_RESP_CLASS: return syncClassHandler;
                case Consts.COMMAND_RESP_STUDENT: return syncStudentHandler;
                case Consts.COMMAND_RESP_TEACHER: return syncTeacherHandler;
                case Consts.COMMAND_RESP_CARD: return syncCardHandler;
                case Consts.COMMAND_RESP_STUDENT_STATE: return syncStudentStateHandler;
                case Consts.COMMAND_RESP_AUTO_SWITCH_TIME: return syncAutoModeSwitchTimeHandler;
                case Consts.COMMAND_RESP_INOUT_STATS: return syncInOutStatsHandler;
                default:break;
            }
        }
        return null;
    }

    @Override
    public boolean isAvailable() {
        return Machine.getInstance().getType() == Machine.MachineType.Gate;
    }

    public class SyncMetaCommandRQ {
        @SerializedName("meta_sync")
        private Map<String, String> metaSync;

        public SyncMetaCommandRQ(Map<String, String> metaSync) {
            this.metaSync = metaSync;
        }

        public Map<String, String> getMetaSync() {
            return this.metaSync;
        }
    }
}
